package com.scanner.net;

import android.content.Context;

import com.scanner.hardware.R;
import com.scanner.util.LogUtil;
import com.google.gson.JsonParseException;
import org.json.JSONException;

import java.io.InterruptedIOException;
import java.net.ConnectException;
import java.net.UnknownHostException;
import java.text.ParseException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

import static com.scanner.net.DefaultObserver.ExceptionReason.CONNECT_ERROR;
import static com.scanner.net.DefaultObserver.ExceptionReason.CONNECT_TIMEOUT;
import static com.scanner.net.DefaultObserver.ExceptionReason.PARSE_ERROR;
import static com.scanner.net.DefaultObserver.ExceptionReason.UNKNOWN_ERROR;


public abstract class DefaultObserver<T extends HttpResult> implements Observer<T> {

    private Context activity;
    //  Activity 是否在执行onStop()时取消订阅


    public DefaultObserver(Context activity) {
        this.activity = activity;
    }



    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(T t) {
        if (t.isStatus()) {
            onSuccess(t);
        } else {
            LogUtil.i("接口请求失败");
            onFail(t.getCode(),t.getMessage());
        }
    }

    @Override
    public void onError(Throwable e) {
        LogUtil.e("网络请求错误："+e.toString());
        if (e instanceof HttpException) {     //   HTTP错误
            onException(ExceptionReason.BAD_NETWORK);
            onFail(InterBase.HTTPEXCEPTION,activity.getString(R.string.connect_error));
        } else if (e instanceof ConnectException
                || e instanceof UnknownHostException) {   //   连接错误
            onException(CONNECT_ERROR);
            onFail(InterBase.CONNECTEXCEPTION,activity.getString(R.string.connect_error));
        } else if (e instanceof InterruptedIOException) {   //  连接超时
            onException(CONNECT_TIMEOUT);
            onFail(InterBase.TIMEOUTEXCEPTION,activity.getString(R.string.connect_timeout));
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {   //  解析错误
            onException(PARSE_ERROR);
            onFail(InterBase.PARSEXCEPTION,activity.getString(R.string.parse_error));
        } else {
            onException(UNKNOWN_ERROR);
            onFail(InterBase.UNKNOWNEXCEPTION,activity.getString(R.string.unknown_error));
        }
    }

    @Override
    public void onComplete() {

    }


    /**
     * 请求成功
     *
     * @param response 服务器返回的数据
     */
    abstract public void onSuccess(T response);

    /**
     * 服务器返回数据，但响应码不为0

     */
    abstract public void onFail(int code,String msg);


    /**
     * 请求异常
     *
     * @param reason
     */
    public void onException(ExceptionReason reason) {
        switch (reason) {
            case CONNECT_ERROR:

                break;

            case CONNECT_TIMEOUT:

                break;

            case BAD_NETWORK:

                break;

            case PARSE_ERROR:

                break;

            case UNKNOWN_ERROR:
            default:

                break;
        }
    }

    /**
     * 请求网络失败原因
     */
    public enum ExceptionReason {
        /**
         * 解析数据失败
         */
        PARSE_ERROR,
        /**
         * 网络问题
         */
        BAD_NETWORK,
        /**
         * 连接错误
         */
        CONNECT_ERROR,
        /**
         * 连接超时
         */
        CONNECT_TIMEOUT,
        /**
         * 未知错误
         */
        UNKNOWN_ERROR,
    }
}
